import request from '@/utils/request'
// 问卷接口
// 问卷新增
export function questionnaireAdd(params) {
  return request({
    url: `/call-center/api/v1/questionnaire`,
    method: 'post',
    data: params
  })
}
// 问卷编辑
export function questionnaireUpdate(params) {
  return request({
    url: `/call-center/api/v1/questionnaire`,
    method: 'put',
    data: params
  })
}
// 问卷发布
export function questionnaireRelease(id) {
  return request({
    url: '/call-center/api/v1/questionnaire/release',
    method: 'POST',
    data: {
      id,
      state: 1
    }
  })
}
// 问卷查询所有
export function questionnaireList() {
  return request({
    url: `/call-center/api/v1/questionnaire/`,
    method: 'get'
  })
}
// 问卷查询分页
export function questionnaireListPage(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/page`,
    method: 'post',
    data: params
  })
}
// 问卷查询根据id
export function questionnaireById(id) {
  return request({
    url: `/call-center/api/v1/questionnaire/` + id,
    method: 'get'
  })
}
// 问卷删除根据id
export function questionnaireDelet(id) {
  return request({
    url: `/call-center/api/v1/questionnaire/` + id,
    method: 'delete'
  })
}
// 问卷删除批量
export function questionnaireDeletBatch(ids) {
  return request({
    url: `/call-center/api/v1/questionnaire/deleteBatch`,
    method: 'delete',
    data: { ids }
  })
}
// 问卷新增(有id返回)
export function questionnaireAddGetId(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/add`,
    method: 'post',
    data: params
  })
}
// 问卷查询详情根据id
export function questionnaireDetailById(id) {
  return request({
    url: `/call-center/api/v1/questionnaire/getQuestionnaireDetail/` + id,
    method: 'get'
  })
}
// 问卷查询详情根据id，并设置答案结构
export function questionnaireDetailByIdSetAnswer(id) {
  // 设置答案结构
  function setAnswerObj(questionnaireChapterDTOList) {
    for (const chapter of questionnaireChapterDTOList) {
      for (const question of chapter.questionnaireQuestionDTOList) {
        question.enterContent = {}
        question.result = ''
        if (['1', 1].includes(question.questionType)) {
          question.result = []
        }
        // 接收输入
        for (let i = 0; i < question.questionOptionDTOList.length; i++) {
          if (question.questionOptionDTOList[i].isEnter === 1) {
            question.enterContent[i] = ''
          }
        }
      }
    }
  }
  return request({
    url: `/call-center/api/v1/questionnaire/getQuestionnaireDetail/` + id,
    method: 'get'
  }).then(res => {
    setAnswerObj(res.data.questionnaireChapterDTOList)
    return res
  })
}
// 问题显示前置条件设置
export function conditionSet(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/condition`,
    method: 'post',
    data: params
  })
}
// 问题新增
export function questionnaireItemAdd(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/item`,
    method: 'post',
    data: params
  })
}
// 问题更新
export function questionnaireItemUpdate(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/item`,
    method: 'put',
    data: params
  })
}
// 问题移动
export function questionnaireItemMove(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/moveItem`,
    method: 'put',
    data: params
  })
}
// 问题删除
export function questionnaireItemDelete(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/item`,
    method: 'delete',
    data: params
  })
}
// 问卷编辑
export function questionnaireSetCondition(params) {
  return request({
    url: `/call-center/api/v1/questionnaire/setCondition`,
    method: 'put',
    data: params
  })
}
